package com.tanjun.publicservice.web.service.impl;

import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.tanjun.common.enums.EnableStatusEnum;
import com.tanjun.common.enums.err.DefaultErrCode;
import com.tanjun.common.exception.BizException;
import com.tanjun.publicservice.web.orm.entity.SysConfig;
import com.tanjun.publicservice.web.orm.mapper.SysConfigMapper;
import com.tanjun.publicservice.web.service.SysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
* @author tanjun
* @description 针对表【sys_config(系统配置表)】的数据库操作Service实现
* @createDate 2025-01-09 21:42:04
*/
@Service
@Slf4j
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig>
    implements SysConfigService{

    /**
     * 新增或者修改系统配置
     *
     * @param sysConfig 系统配置数据
     */
    @Override
    @CacheEvict(cacheNames = {"getSysConfigValueByKey","getSysConfigByKey"},allEntries = true)
    public void saveOrUpdateConfig(SysConfig sysConfig) {
        long count=this.count(QueryChain.of(SysConfig.class)
                .ne(SysConfig::getId,sysConfig.getId(),sysConfig.getId()!=null)
                .eq(SysConfig::getConfigKey,sysConfig.getConfigKey())
        );
        if(count>1){
            throw new BizException("系统配置编码有已存在");
        }
        if(sysConfig.getId()!=null){
         SysConfig old=this.getById(sysConfig.getId());
         if(old==null){
             throw new BizException(DefaultErrCode.NO_DATA);
         }
         this.updateById(sysConfig);
        }
        else {
            this.save(sysConfig);
        }
    }

    /**
     * 根据id 删除系统配置数据
     *
     * @param ids 配置ids
     */
    @Override
    @CacheEvict(cacheNames = {"getSysConfigValueByKey","getSysConfigByKey"},allEntries = true)
    public void delSysConfigByIds(List<Long> ids) {
        this.removeByIds(ids);
    }

    /**
     * 根据key来获取配置value
     *
     * @param key 配置key
     * @return 配置value
     */
    @Override
    @Cacheable(value = "getSysConfigValueByKey")
    public String getSysConfigValueByKey(String key) {
       SysConfig config= this.getOne(QueryChain.of(SysConfig.class)
                        .select(SysConfig::getConfigValue)
                .eq(SysConfig::getConfigKey,key)
                .eq(SysConfig::getStatus, EnableStatusEnum.YES)
        );
       return Optional.ofNullable(config)
               .map(SysConfig::getConfigValue)
               .orElse(null);
    }

    /**
     * 根据key获取配置信息
     *
     * @param key 缓存key
     * @return 缓存信息
     */
    @Override
    @Cacheable(value = "getSysConfigByKey")
    public SysConfig getSysConfigByKey(String key) {
        return this.getOne(QueryChain.of(SysConfig.class)
                .select(SysConfig::getConfigValue)
                .eq(SysConfig::getConfigKey,key)
                .eq(SysConfig::getStatus, EnableStatusEnum.YES)
        );
    }
    /**
     * 清除缓存
     */
    @Override
    @CacheEvict(cacheNames = {"getSysConfigValueByKey","getSysConfigByKey"},allEntries = true)
    public void clearCache() {
        log.info("缓存已清理");
    }



}




